package script.learn
{
	
	import laya.events.Event;
	import laya.media.SoundManager;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.lesson.EnglishWordVo;
	import model.lesson.LearnEnglishModel;
	
	import script.baseView.BaseViewController;
	import script.learn.item.SentenceCell;
	
	import ui.learn.LearnWordsPanelUI;
	
	public class LearnWordsController extends BaseViewController
	{
		private var uiSkin:LearnWordsPanelUI;

		private var param:Object;
		
		private var curWordIndex:int;
		public function LearnWordsController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as LearnWordsPanelUI;
			
			uiSkin.sentencelist.itemRender = SentenceCell;
			
			//uiSkin.orderList.vScrollBarSkin = "";
			uiSkin.sentencelist.repeatX = 1;
			uiSkin.sentencelist.spaceY = 10;
			
			uiSkin.sentencelist.renderHandler = new Handler(this, updateSentencelist);
			uiSkin.sentencelist.selectEnable = false;
			uiSkin.sentencelist.array = [];
			
			uiSkin.title.text = LearnEnglishModel.instance.classlevel.levelComment + "(" + LearnEnglishModel.instance.lessondata.className + ")" + "\n" +  LearnEnglishModel.instance.lessondata.classComment;
			if(LearnEnglishModel.instance.lessondata != null)
			{
				uiSkin.title.text = LearnEnglishModel.instance.classlevel.levelComment + "(" + LearnEnglishModel.instance.lessondata.className + ")" + "\n" +  LearnEnglishModel.instance.lessondata.classComment;

				var postdata:String = "lessonId=" + LearnEnglishModel.instance.lessondata.id;
				HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getLessonWords,this,onGetLessonWordsBack,postdata,"post");
			}
			
			this.uiSkin.speakerenglish.on(Event.CLICK,this,onSpeakEnglish);
			this.uiSkin.englishtxt.on(Event.CLICK,this,onSpeakEnglish);

			this.uiSkin.speakerch.on(Event.CLICK,this,onSpeakChinese);
			this.uiSkin.chinesetxt.on(Event.CLICK,this,onSpeakChinese);

			this.uiSkin.prebtn.on(Event.CLICK,this,onPrevWord);
			this.uiSkin.nextbtn.on(Event.CLICK,this,onNextWord);


		}
		
		private function onGetLessonWordsBack(data:*):void
		{
			if(this.destroyed)
				return;
			
			var result:Object = JSON.parse(data);
			if(result.code == HttpErrorCode.OK)
			{
				LearnEnglishModel.instance.initLearnData(result.wordlist as Array);
				
				curWordIndex = 0;
				
				changeWord();
				//trace("ssss");
				//uiSkin.lessonlist.array = result.lessonlist;
			}
		}
		
		private function changeWord():void
		{
			if(LearnEnglishModel.instance.leanWords.length > curWordIndex)
			{
				var word:EnglishWordVo = LearnEnglishModel.instance.leanWords[curWordIndex];
				
				this.uiSkin.englishtxt.text = word.englishWord;
				
				this.uiSkin.chinesetxt.text = word.chineseWord;
				
				this.uiSkin.sentencelist.array = word.sentenceList;
				
				this.uiSkin.pagetxt.text = (curWordIndex+1) + "/" + LearnEnglishModel.instance.leanWords.length;
			}
		}
		
		private function onPrevWord():void
		{
			if(curWordIndex > 0)
			{
				curWordIndex--;
				changeWord();
			}
		}
		
		private function onNextWord():void
		{
			if(curWordIndex < LearnEnglishModel.instance.leanWords.length - 1)
			{
				curWordIndex++;
				changeWord();
			}
		}
		
		private function onSpeakEnglish():void
		{
			if(LearnEnglishModel.instance.leanWords.length > curWordIndex)
			{
				var word:EnglishWordVo = LearnEnglishModel.instance.leanWords[curWordIndex];
				
				SoundManager.playSound("sound/words/" + word.engSoundRes);
			}
		}
		
		private function onSpeakChinese():void
		{
			if(LearnEnglishModel.instance.leanWords.length > curWordIndex)
			{
				var word:EnglishWordVo = LearnEnglishModel.instance.leanWords[curWordIndex];
				
				SoundManager.playSound("sound/words/" + word.chSoundRes);
			}
		}
		public function updateSentencelist(cell:SentenceCell):void
		{
			cell.setData(cell.dataSource);
		}
		
	}
}